from multiprocessing import Process, Lock
from random import random as rand
import time

tic = time.time()

def myFunction(l, idx):
    import neuron as n
    sec = n.h.Section()
    number =  int((rand()) * 10)
    time.sleep(number)
    toc = time.time()
    l.acquire(block=True, timeout=None)
    print 'Number %s slept for %s (~%s ?) seconds (%s)' %(
        idx, toc-tic, number, sec.name) 
    l.release()

if __name__ == '__main__':
    # use `Lock` in order to ensure that only one process prints to standard
    # output at a time
    lock = Lock()
    ps = []
    for idx in range(10):
        p = Process(target=myFunction, args=(lock, idx))
        p.start()
        ps.append(p)
    for p in ps:
      p.join()
    print "done"
